package com.gshg.log.cleanlog.utils;



import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 产生随机数或者随机字符串工具类
 */
@Slf4j
public class RandomUtil {

    static String[] lowCase = {"a","b","c","d","e", "f", "g",
            "h", "i", "j", "k", "l", "m", "n", "o", "p", "q","r", "s", "t",
            "u", "v", "w", "x", "y", "z"};
    static String[] upCase = {"A","B","C","D","E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q","R", "S", "T",
            "U", "V", "W", "X", "Y", "Z", "_"};
    static String[] numArray={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

    public static double randomDoubleNum(){
        double res = Math.random();
        return res;
    }

    public static int randomIntNum(){
        Random r = new Random();
        int res = r.nextInt(9999999);
        return  res;
    }

    /**
     * 产生4位小写字母字符串
     * @return
     */
    public static String randomStr4(){
        String str="";
        for(int a=0; a<4; a++){
            int i = randomIntNum()%26;
            str = str + lowCase[i];
        }
        return str;
    }

    /**
     * 产生12位密码，包含数字、大写字母、小写字母
     * @return
     */
    public static String randomStrPassword(){
        String str="";
        for(int a=0; a<4; a++){
            int i = randomIntNum();
            int j = i%10;
            int k = i%26;
            int l = i%27;
            str = str + upCase[l];
            str = str + lowCase[k];
            str = str + numArray[j];
        }
        return str;
    }

    /**
     * 产生指定位数的数字
     * @param length   位数
     * @return
     */
    public static String numStr(int length){
        if (length == 0){
            length = 3;
        }
        String str = "";
        for(int i=0; i<length; i++){
            int ranNum = randomIntNum()%10;
            str = str + numArray[ranNum];
        }
        return str;
    }

    /**
     * 在指定范围内产生随机数，int类型
     * @param min
     * @param max
     * @return
     */
    public static int randomIntNum(int min, int max){
        if(min > max){
            int temp = max;
            max = min;
            min = temp;
        }
        Random r = new Random();
        int res = r.nextInt(max-min+1)+min;
        return  res;
    }



    /**
     * 在指定范围内产生随机数，int类型
     * @param min
     * @param max
     * @return
     */
    public static double randomDoubleNum(double min, double max){
        if(min > max){
            double temp = max;
            max = min;
            min = temp;
        }
//        double res = r.nextInt(max-min+1)+min;
        double res = RandomUtils.nextDouble(min, max);
        return  res;
    }

    /**
     * 在指定范围内产生随机数，int类型
     * @param min
     * @param max
     * @return
     */
    public static double randomDoubleNum0To1(double min, double max){
        if(min > max){
            double temp = max;
            max = min;
            min = temp;
        }

        if(min<0 || min>1){
            min = 0.58;
        }

        if(max<0 || max>1){
            max = 0.88;
        }

        double res = RandomUtils.nextDouble(min, max);
        return  res;
    }

    /**
     * 获取随机日期
     *
     * @param beginDate
     *            起始日期，格式为：yyyyMMddHHmmss
     * @param endDate
     *            结束日期，格式为：yyyyMMddHHmmss
     * @return
     */

    public static String randomDate(String beginDate, String endDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            Date start = format.parse(beginDate);// 构造开始日期
            Date end = format.parse(endDate);// 构造结束日期
            // getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
            long startLong = start.getTime();
            long endLong = end.getTime();
            if (startLong >= endLong) {
                long temp = endLong;
                endLong = startLong;
                startLong = temp;
            }
            long date = random(startLong, endLong) ;
            return format.format(new Date(date)) ;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            long curTime = System.currentTimeMillis();
            curTime = curTime - randomIntNum(600*1000, 36000*1000);
            Date dateCur = new Date(curTime);
            return format.format(dateCur);
        }
    }



    /**
     * 获取随机日期
     *
     * @param beginDate
     *            起始日期，格式为：yyyyMMddHHmmss
     * 结束时间为当前时间 减去5分钟到20分钟
     * @return
     */

    public static String randomDate(String beginDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            long curDateLong = System.currentTimeMillis();
            curDateLong = curDateLong - randomIntNum(5*60*1000, 20*60*1000);
            Date start = format.parse(beginDate);// 构造开始日期
            Date end = new Date(curDateLong);// 构造结束日期
            // getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
            long startLong = start.getTime();
            long endLong = end.getTime();
            if (startLong >= endLong) {
                long temp = endLong;
                endLong = startLong;
                startLong = temp;
            }
            long date = random(startLong, endLong) ;
            return format.format(new Date(date)) ;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            long curTime = System.currentTimeMillis();
            curTime = curTime - randomIntNum(600*1000, 36000*1000);
            Date dateCur = new Date(curTime);
            return format.format(dateCur);
        }
    }


    /**
     * 根据传入的日期随机补充时分秒
     *
     * @param dateStr   格式为yyyyMMdd
     * @return  返回格式为：yyyyMMddHHmmss
     */
    public static String randomDateNewTime(String dateStr){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date nowDate = new Date();
        try{
            dateStr = sdf.format(sdf.parse(dateStr));
            String curTimeStr = sdf.format(nowDate);
            long dateParamLong = Long.parseLong(dateStr);
            long curTimeLong = Long.parseLong(curTimeStr);
            if(curTimeLong < dateParamLong){
                dateStr = curTimeStr;
            }

            long nowTimeLong = nowDate.getTime();
            nowTimeLong = nowTimeLong - randomIntNum(600*1000, 3600*1000);
            Date dateCur = new Date(nowTimeLong);
            String curTime = dateFormat.format(dateCur);
            String result = StringUtils.overlay(curTime, dateStr, 0,8);
            Date tempDate = dateFormat.parse(result);
            return dateFormat.format(tempDate);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            long curTime = System.currentTimeMillis();
            curTime = curTime - randomIntNum(600*1000, 36000*1000);
            Date dateCur = new Date(curTime);
            return dateFormat.format(dateCur);
        }

    }

    private static long random(long begin, long end) {
        long rtn = begin + (long) (Math.random() * (end - begin));
        // 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值
        if (rtn == begin || rtn == end) {
            return random(begin, end);
        }
        return rtn;
    }
}
